//
//  GUATool.m
//  ShenghuoJia
//
//  Created by Guava on 15/12/16.
//  Copyright © 2015年 YongHui. All rights reserved.
//

#import "GUATool.h"
#import "AppDelegate.h"
#import "UIAlertView+GuaUIAlertView.h"
#import "UIAlertController+Guava.h"


@implementation GUATool

+ (void)showDialog:(NSString *)title message:(NSString *)message waitingTime:(NSTimeInterval)time {
   
    dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
        [((AppDelegate *)[UIApplication sharedApplication].delegate).window.rootViewController presentViewController:alertController animated:YES completion:nil];
        [alertController performSelector:@selector(dismissViewControllerAnimated:) withObject:@(YES) afterDelay:time];
    });
    
}

+(CGFloat)calculateLabelHeightWithWidth:(CGFloat) widthOfLabel
                            contentText:(NSString *) content
                                   font:(UIFont *)contentFont
{
    CGRect frame = [content boundingRectWithSize:CGSizeMake(widthOfLabel, 100)
                                         options:NSStringDrawingUsesLineFragmentOrigin
                                      attributes:@{NSFontAttributeName:contentFont}
                                         context:nil];
    return frame.size.height +4;
}
+(CGFloat)calculateLabelWidthWithHeight:(CGFloat) heightOfLabel
                            contentText:(NSString *) content
{
    return 0;
}

+ (BOOL)checkUserIdCard: (NSString *) idCard
{
    NSString *pattern = @"(^[0-9]{15}$)|([0-9]{17}([0-9]|X)$)";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:idCard];
    return isMatch;
}

+ (BOOL)simpleYHCard:(NSString *)num {
    NSString *pattern = @"^\\d{13}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pattern];
    return [pred evaluateWithObject:num];
}

+(BOOL)simpleCellphoneNumberCheck:(NSString *)cellNumber {
    NSString *pattern = @"^[1-9]\\d{10}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pattern];
    return [pred evaluateWithObject:cellNumber];
}

+ (BOOL)isEmptyString:(NSString *)aString {
    if([aString isEqualToString:@""] || aString == nil)
        return YES;
    return NO;
}

+ (NSData *)dataWithImage:(UIImage *)image {
    return UIImageJPEGRepresentation(image, 0.6);
}
+ (BOOL)valiMobile:(NSString *)mobile{
    if (mobile.length < 11)
    {
        return NO;
    }else{
        /**
         * 移动号段正则表达式
         */
        NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
        /**
         * 联通号段正则表达式
         */
        NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
        /**
         * 电信号段正则表达式
         */
        NSString *CT_NUM = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";
        NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM_NUM];
        BOOL isMatch1 = [pred1 evaluateWithObject:mobile];
        NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM];
        BOOL isMatch2 = [pred2 evaluateWithObject:mobile];
        NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM];
        BOOL isMatch3 = [pred3 evaluateWithObject:mobile];
        
        if (isMatch1 || isMatch2 || isMatch3) {
            return YES;
        }else{
            return NO;
        }
    }
}

+(NSString *)stringFromHTMLString:(NSString *)HTMLString {
    return [HTMLString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
@end
